package com.xoa.controller.crm;

import com.xoa.dao.crm.SupplierMapper;
import com.xoa.model.crm.Product;
import com.xoa.service.crm.ProductService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xoa.model.crm.Supplier;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by caoyin on 2019/5/29.
 * 产品管理[crm_product]
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    //映射主页面[显示产品信息首页]
    @RequestMapping("/showProduct")
    public String showProduct() {
        return "/app/crm/product/showProduct";
    }

    //修改产品
    @RequestMapping("/updateProduct")
    public String updateProduct() {
        return "/app/crm/product/updateProduct";
    }

    //添加页面
    @RequestMapping("/addProduct")
    public String addProduct() {
        return "/app/crm/product/addProduct";
    }

    // 详情页面
    @RequestMapping("/detailsProduct")
    public String detailsProduct() {
        return "app/crm/product/detailsProduct";
    }

    // 导入页面
    @RequestMapping("/importProduct")
    public String importProduct() {
        return "app/crm/product/importProduct";
    }

    //注入service层
    @Autowired
    private ProductService productService;

    //注入供应商表mapper
    @Resource
    private SupplierMapper supplierMapper;


    /**
     * 查询所有(不带参数)[导出功能接口]
     * @param request
     * @param page
     * @param pageSize
     * @param useFlag
     * @return
     */
    @RequestMapping("/selectAllProduct")
    @ResponseBody
    public ToJson<Product> selectAll(HttpServletRequest request, Integer page , Integer pageSize, boolean useFlag){
        ToJson json =new ToJson<Product>(1,"err");
        Map<String, Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page", pageParams);
        try {
            List<Product> data = productService.selectAll();
            if(data!=null){
                json.setTotleNum(pageParams.getTotal());
                json.setObj(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询所有(没有被删除的
     * @param request
     * @param page
     * @param pageSize
     * @param useFlag
     * @return
     */
    @RequestMapping("/selectProduct")
    @ResponseBody
    public ToJson<Product> selectProduct(HttpServletRequest request, Integer page , Integer pageSize, boolean useFlag){
        ToJson json =new ToJson<Product>(1,"err");
        Map<String, Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        map.put("page", pageParams);
        try {
            List<Product> data = productService.selectProduct();
            if(data!=null){
                json.setTotleNum(pageParams.getTotal());
                json.setObj(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 模糊查询及分页
     * @param request
     * @param page
     * @param pageSize
     * @param useFlag
     * @param product
     * @return
     */
    @RequestMapping("/selectByConditionProduct")
    @ResponseBody
    public ToJson<Product> selectByCondition(HttpServletRequest request, Integer page, Integer pageSize, Boolean useFlag, Product product){
        Map<String,Object> map= new HashMap<String, Object>();
        product.setIsDeleted(Byte.valueOf("0"));
        map.put("obj",product);
        ToJson json =new ToJson<Product>(1,"err");
        try {
            List<Product> productList =productService.selectByCondition(page, pageSize, useFlag, map);
            //循环遍历出提供商名称
            for(Product data : productList){
                Supplier supplier = supplierMapper.selectByPrimaryKey(data.getSupplierId());
                if(supplier != null){
                    data.setSupplierName(supplier.getSupplierName());
                }
            }
            if(productList!=null){
                PageParams pageParams = (PageParams) map.get("page");
                json.setTotleNum(pageParams.getTotal());
                json.setObj(productList);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            json.setMsg(e.getMessage());
            json.setFlag(1);
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 数据回显接口
     * @param request
     * @param productId
     * @return
     */
    @RequestMapping("/dataDetail")
    @ResponseBody
    public ToJson<Product> selectByPrimaryKey(HttpServletRequest request, Integer productId){
        ToJson json =new ToJson<Product>(1,"err");
        try {
            Product data =productService.selectByPrimaryKey(productId);
            if(data.getSupplierId() != null){
                Supplier supplier = supplierMapper.selectByPrimaryKey(data.getSupplierId());
                data.setSupplierName(supplier.getSupplierName());
            }
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据主键id进行删除(逻辑删除)
     * 删除标志(isDeleted)：1-已删除;0-未删除(默认未删除)
     * @param request
     * @param productId
     * @return
     */
    @RequestMapping("/delProductById")
    @ResponseBody
    public ToJson<Integer> deleteByPrimaryKey(HttpServletRequest request, Integer productId){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =productService.deleteByPrimaryKey(productId);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 添加产品数据
     * @param request
     * @param product
     * @return
     */
    @RequestMapping("/insertProduct")
    @ResponseBody
    public ToJson<Integer> insertProduct(HttpServletRequest request, Product product){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =productService.insertProduct(product);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 更新数据(根据产品id)
     * @param product
     * @return
     */
    @RequestMapping("/updateProductById")
    @ResponseBody
    public ToJson<Integer> updateByPrimaryKeySelective(Product product){
        ToJson json =new ToJson<Integer>(1,"err");
        try {
            Integer data =productService.updateByPrimaryKeySelective(product);
            if(data!=null){
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 产品信息表的导出
     * @param request
     * @param response
     * @param product
     * @param beginDate
     * @param isExport
     * @return
     */
    @RequestMapping(value = "/queryExportProduct", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<Product> queryExportProduct(HttpServletRequest request, HttpServletResponse response, Product product, String beginDate, String isExport) {
        return productService.queryExportProduct(request, response,product, beginDate, isExport);
    }

    /**
     * 产品信息表的导入
     * @param request
     * @param response
     * @param file
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/queryImportProduct", produces = {"application/json;charset=UTF-8"})
    public ToJson<Product> insertImport(HttpServletRequest request, HttpServletResponse response, MultipartFile file) {
        return productService.queryImportProduct(request,response,file);
    }

}
